package com.wsd.smartcarwasher.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.wsd.smartcarwasher.constans.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SMSService {

    //网易云信分配的账号，请替换你在管理后台应用下申请的Appkey
    @Value("${sms.APP_KEY}")
    private  String  APP_KEY;
    //网易云信分配的密钥，请替换你在管理后台应用下申请的appSecret
    @Value("${sms.APP_SECRET}")
    private  String APP_SECRET;// = "cce48e21cc7a";

    private static final Logger logger = LoggerFactory.getLogger(SMSService.class);

    public void sendCardMSG(String mobile,String cadName,String aTime,double aMoney) {
        String TemplateParam = "{'member':'cadName','money':'aMoney','time':'aTime'}";
        TemplateParam = TemplateParam.replace("cadName",cadName)
                .replace("aMoney",String.valueOf(aMoney)).replace("aTime",aTime);
        try {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                    APP_KEY , APP_SECRET);
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", mobile);
            request.putQueryParameter("SignName", "陕西漫蜗智能洗车");
            request.putQueryParameter("TemplateCode", Constant.SMS_TEMPLATE_CARD);
            request.putQueryParameter("TemplateParam", TemplateParam);
            CommonResponse response = client.getCommonResponse(request);
            logger.info("购买会员卡发送短息");
            logger.info(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void sendCouponMSG(String mobile,String couponName,String aTime) {
        String TemplateParam = "{'name':'couponName','time':'aTime'}";
        TemplateParam = TemplateParam.replace("couponName",couponName)
                .replace("aTime",aTime);
        try {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                    APP_KEY , APP_SECRET);
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", mobile);
            request.putQueryParameter("SignName", "陕西漫蜗智能洗车");
            request.putQueryParameter("TemplateCode", Constant.SMS_TEMPLATE_COUPON);
            request.putQueryParameter("TemplateParam", TemplateParam);
            CommonResponse response = client.getCommonResponse(request);
            logger.info("管理员发放优惠卷发送短息");
            logger.info(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
